package com.ymatou.iapi.idcardmng.testcase;

import java.io.File;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.idcardmng.parameter.SubmitIdCardInfoAndPicBean;
import com.ymatou.iapi.idcardmng.service.SubmitIdCardInfoAndPicCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.sqlwapper.YmtIdCardInfoWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
 * 上传身份证信息和图片接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SubmitIdCardInfoAndPic {
	private static SubmitIdCardInfoAndPicBean submitidcardinfoBean;
	private static SubmitIdCardInfoAndPicCall submitidcardinfoCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("上传身份证信息和图片接口");
	}

	@Before
	public void caseUp() {
		submitidcardinfoBean = new SubmitIdCardInfoAndPicBean();
		submitidcardinfoCall = new SubmitIdCardInfoAndPicCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(submitidcardinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDb() throws Exception {

		long buyerId = submitidcardinfoBean.getBuyerId();
		YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();
		String oldKey = "1234567890123456";
		String newKey = "123456789";

		YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

		Logger.comment("校验身份证信息表ymt_idcardinfo");

		Logger.verifyEquals((int) buyerId, ymtIdCardInfo.getBuyerId(),
				"校验buyerid");

		Logger.verifyEquals(submitidcardinfoBean.getName(),
				ymtIdCardInfo.getName(), "校验name");

		Logger.verifyEquals(submitidcardinfoBean.getCardType(),
				ymtIdCardInfo.getCardType(), "校验CardType");

		Logger.verifyEquals(0, ymtIdCardInfo.getStatus(), "校验Status");
		Logger.verifyEquals(submitidcardinfoBean.isHasPic(),
				ymtIdCardInfo.getHasPic(), "校验HasPic");
		Logger.verifyEquals(submitidcardinfoBean.getOperater(),
				ymtIdCardInfo.getOperater(), "校验Operater");

		Logger.verifyEquals(0, ymtIdCardInfo.getRejectReason(),
				"校验RejectReason");
		Logger.verifyEquals(0, ymtIdCardInfo.getIsDelete(), "校验IsDelete");

		Logger.verifyEquals(
				CipherUtil.encryptAES(oldKey,
						submitidcardinfoBean.getCardNumber()),
				ymtIdCardInfo.getIDCard(), "校验IDCard");
		Logger.verifyEquals(
				ymtIdCardInfo.getPhone() != null ? CipherUtil.encryptAES(
						oldKey, submitidcardinfoBean.getMobile()) : null,
				ymtIdCardInfo.getPhone(), "校验phone");

		if (ymtIdCardInfo.getPhone() != null) {
			
			String plainText = CipherUtil.decryptAES(oldKey,
					ymtIdCardInfo.getPhone());
			Logger.verifyEquals(CipherUtil.encryptAES(newKey, plainText),
					ymtIdCardInfo.getMobile(), "校验Mobile");
			Logger.verifyEquals(plainText, ymtIdCardInfo.getTempPhone(),
					"校验TempPhone");
			
		} else {
			
			Logger.verifyEquals(CipherUtil.encryptAES(newKey, submitidcardinfoBean.getMobile()),
					ymtIdCardInfo.getMobile(), "校验Mobile");
			
		}
		
		Logger.verifyEquals(submitidcardinfoBean.getCheckIdCardStatus(),
				ymtIdCardInfo.getCheckIdCardStatus(), "校验CheckIdCardStatus");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_001() {
		Logger.start(true, "上传身份证信息和图片成功,发送mq消息通知交易,贝海");
		try {

			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCard.jpg";

			String fliename2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCardOpposite.jpg";

			submitidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			submitidcardinfoBean.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitidcardinfoBean.setCardType(1);
			submitidcardinfoBean.setHasPic(true);
			submitidcardinfoBean.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setName(TestDataManager.getChineseName());
			submitidcardinfoBean.setOperater("autotest");

			submitidcardinfoBean.setReverseSideBase64(TestDataManager
					.getBase64(fliename2));

			submitidcardinfoBean.setRightSideBase64(TestDataManager
					.getBase64(fliename));

			submitidcardinfoBean.setCheckIdCardStatus(200);

			submitidcardinfoCall.setData(submitidcardinfoBean);
			submitidcardinfoCall.callService();
			Logger.verifyEquals("200", submitidcardinfoCall.getString("Code"),
					"验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_002() {
		Logger.start(true, "用户id,与姓名在本地库存在,提交更新身份证号码成功,发送mq消息通知交易,贝海");
		try {

			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCard.jpg";

			String fliename2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCardOpposite.jpg";

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();
			;

			YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = ymtIdCardInfo.getName();

			submitidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setBuyerId(buyerId);
			submitidcardinfoBean.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitidcardinfoBean.setCardType(1);
			submitidcardinfoBean.setHasPic(true);
			submitidcardinfoBean.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setName(name);
			submitidcardinfoBean.setOperater("autotest");
			submitidcardinfoBean.setReverseSideBase64(TestDataManager
					.getBase64(fliename2));

			submitidcardinfoBean.setRightSideBase64(TestDataManager
					.getBase64(fliename));

			submitidcardinfoBean.setCheckIdCardStatus(200);

			submitidcardinfoCall.setData(submitidcardinfoBean);
			submitidcardinfoCall.callService();

			Logger.verifyEquals("200", submitidcardinfoCall.getString("Code"),
					"验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_003() {
		Logger.start(true, "买家Id为null");
		try {

			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCard.jpg";

			String fliename2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCardOpposite.jpg";

			submitidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			/*
			 * submitidcardinfoBean.setBuyerId(Long.valueOf(EnvSetup
			 * .getData("SellerId2")));
			 */
			submitidcardinfoBean.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitidcardinfoBean.setCardType(1);
			submitidcardinfoBean.setHasPic(true);
			submitidcardinfoBean.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setName(TestDataManager.getChineseName());
			submitidcardinfoBean.setOperater("autotest");

			submitidcardinfoBean.setReverseSideBase64(TestDataManager
					.getBase64(fliename2));

			submitidcardinfoBean.setRightSideBase64(TestDataManager
					.getBase64(fliename));

			submitidcardinfoBean.setCheckIdCardStatus(200);

			submitidcardinfoCall.setData(submitidcardinfoBean);
			submitidcardinfoCall.callService();

			Logger.verifyEquals("400", submitidcardinfoCall.getString("Code"),
					"验证Code");

			Logger.verifyEquals("100", submitidcardinfoCall.getString("BCode"),
					"验证BCode");

			Logger.verifyEquals("buyerId:买家Id不能为空",
					submitidcardinfoCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_004() {
		Logger.start(true, "身份证正面 base64 格式rightSide字符串为null");
		try {

			/*
			 * String fliename = System.getProperty("user.dir") + File.separator
			 * + "resource" + File.separator + "idCard.jpg";
			 */

			String fliename2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCardOpposite.jpg";

			submitidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			submitidcardinfoBean.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitidcardinfoBean.setCardType(1);
			submitidcardinfoBean.setHasPic(true);
			submitidcardinfoBean.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setName(TestDataManager.getChineseName());
			submitidcardinfoBean.setOperater("autotest");

			submitidcardinfoBean.setReverseSideBase64(TestDataManager
					.getBase64(fliename2));

			/*
			 * submitidcardinfoBean.setRightSideBase64(TestDataManager
			 * .getBase64(fliename));
			 */

			submitidcardinfoBean.setCheckIdCardStatus(200);

			submitidcardinfoCall.setData(submitidcardinfoBean);
			submitidcardinfoCall.callService();

			Logger.verifyEquals("500", submitidcardinfoCall.getString("Code"),
					"验证Code");

			Logger.verifyEquals("图片不能为空!",
					submitidcardinfoCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_005() {
		Logger.start(true, "身份证反面 base64格式reverseSide字符串为null");
		try {

			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCard.jpg";

			/*
			 * String fliename2 = System.getProperty("user.dir") +
			 * File.separator + "resource" + File.separator +
			 * "idCardOpposite.jpg";
			 */

			submitidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			submitidcardinfoBean.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitidcardinfoBean.setCardType(1);
			submitidcardinfoBean.setHasPic(true);
			submitidcardinfoBean.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setName(TestDataManager.getChineseName());
			submitidcardinfoBean.setOperater("autotest");

			/*
			 * submitidcardinfoBean.setReverseSideBase64(TestDataManager
			 * .getBase64(fliename2));
			 */

			submitidcardinfoBean.setRightSideBase64(TestDataManager
					.getBase64(fliename));

			submitidcardinfoBean.setCheckIdCardStatus(200);

			submitidcardinfoCall.setData(submitidcardinfoBean);
			submitidcardinfoCall.callService();

			Logger.verifyEquals("500", submitidcardinfoCall.getString("Code"),
					"验证Code");

			Logger.verifyEquals("图片不能为空!",
					submitidcardinfoCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_006() {
		Logger.start(true, "篡改base64图片字符串");
		try {

			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCard.jpg";

			String fliename2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCardOpposite.jpg";

			submitidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			submitidcardinfoBean.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitidcardinfoBean.setCardType(1);
			submitidcardinfoBean.setHasPic(true);
			submitidcardinfoBean.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setName(TestDataManager.getChineseName());
			submitidcardinfoBean.setOperater("autotest");

			String base64String = TestDataManager.getBase64(fliename2);

			submitidcardinfoBean.setReverseSideBase64(base64String.substring(0,
					1000));

			submitidcardinfoBean.setRightSideBase64(TestDataManager
					.getBase64(fliename));

			submitidcardinfoBean.setCheckIdCardStatus(200);

			submitidcardinfoCall.setData(submitidcardinfoBean);
			submitidcardinfoCall.callService();

			Logger.verifyEquals("200", submitidcardinfoCall.getString("Code"),
					"验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_007() {
		Logger.start(true, "证件号为null");
		try {

			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCard.jpg";

			String fliename2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCardOpposite.jpg";

			submitidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			/*
			 * submitidcardinfoBean .setCardNumber(TestDataManager.getCardId(20,
			 * 10));
			 */
			submitidcardinfoBean.setCardType(1);
			submitidcardinfoBean.setHasPic(true);
			submitidcardinfoBean.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setName(TestDataManager.getChineseName());
			submitidcardinfoBean.setOperater("autotest");

			submitidcardinfoBean.setReverseSideBase64(TestDataManager
					.getBase64(fliename2));

			submitidcardinfoBean.setRightSideBase64(TestDataManager
					.getBase64(fliename));

			submitidcardinfoBean.setCheckIdCardStatus(200);

			submitidcardinfoCall.setData(submitidcardinfoBean);
			submitidcardinfoCall.callService();
			Logger.verifyEquals("400", submitidcardinfoCall.getString("Code"),
					"验证Code");
			Logger.verifyEquals("100", submitidcardinfoCall.getString("BCode"),
					"验证BCode");
			Logger.verifyEquals("cardNumber:证件号不能为空",
					submitidcardinfoCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_009() {
		Logger.start(true, "姓名name为null");
		try {

			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCard.jpg";

			String fliename2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCardOpposite.jpg";

			submitidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			submitidcardinfoBean.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitidcardinfoBean.setCardType(1);
			submitidcardinfoBean.setHasPic(true);
			submitidcardinfoBean.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			/* submitidcardinfoBean.setName(TestDataManager.getChineseName()); */
			submitidcardinfoBean.setOperater("autotest");

			submitidcardinfoBean.setReverseSideBase64(TestDataManager
					.getBase64(fliename2));

			submitidcardinfoBean.setRightSideBase64(TestDataManager
					.getBase64(fliename));

			submitidcardinfoBean.setCheckIdCardStatus(200);

			submitidcardinfoCall.setData(submitidcardinfoBean);
			submitidcardinfoCall.callService();
			Logger.verifyEquals("400", submitidcardinfoCall.getString("Code"),
					"验证Code");
			Logger.verifyEquals("100", submitidcardinfoCall.getString("BCode"),
					"验证BCode");
			Logger.verifyEquals("name:名称不能为空",
					submitidcardinfoCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_010() {
		Logger.start(true, "模拟图片服务超时未响应");
		try {

			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCard.jpg";

			String fliename2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCardOpposite.jpg";

			submitidcardinfoBean.setBillCode(TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			submitidcardinfoBean.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitidcardinfoBean.setCardType(1);
			submitidcardinfoBean.setHasPic(true);
			submitidcardinfoBean.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitidcardinfoBean.setName(TestDataManager.getChineseName());
			submitidcardinfoBean.setOperater("autotest");

			submitidcardinfoBean.setReverseSideBase64(TestDataManager
					.getBase64(fliename2));

			submitidcardinfoBean.setRightSideBase64(TestDataManager
					.getBase64(fliename));
			
			submitidcardinfoBean.setCheckIdCardStatus(200);

			submitidcardinfoCall.setData(submitidcardinfoBean);
			submitidcardinfoCall.callService();
			Logger.verifyEquals("200", submitidcardinfoCall.getString("Code"),
					"验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) throws Exception {
		String plainText = CipherUtil.decryptAES("123456789",
				"9FBFD298A27CB3518768C27EB2472B45");
		
		System.out.println(plainText);
	}
}